﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.IO;
using common;

namespace common.web
{
    public class inc
    {
        public static bool safestr(string str, string zh)
        {
            if (zh == null) return func.chkRegex(str, @"^[A-Za-z0-9-_]{4,20}$");
            return func.chkRegex(str, @"^[A-Za-z0-9-_\u4e00-\u9fa5]{2,20}$");
        }

        public static void chkyzm(string bdm)
        {
            string _vCode = func.NoNull(HttpContext.Current.Request.Form[bdm]).Trim();
            if (HttpContext.Current.Session["v_Code"] == null || _vCode.ToUpper() != HttpContext.Current.Session["v_Code"].ToString())
            {
                func.Alert("验证码不正确!", null);
            }
        }

        public static bool chkyzmb(string bdm)
        {
            string _vCode = func.NoNull(HttpContext.Current.Request.Form[bdm]).Trim();
            if (HttpContext.Current.Session["v_Code"] == null || _vCode.ToUpper() != HttpContext.Current.Session["v_Code"].ToString())
            {
                return false;
            }
            return true;
        }

        public static string chkszje(string inpstr, string tsstr)  //判断小数是否合法
        {
            inpstr = func.NoNull(inpstr);
            if (!func.chkRegex(inpstr, @"^\d{1,8}(\.\d{1,2})?$")) return tsstr + "必须为数字格式!";
            if (double.Parse(inpstr) <= 0) return tsstr + "金额必须大于0!";
            return "1";
        }

        public static string getszje(string inpstr, string tsstr)  //输出合法的小数
        {
            inpstr = func.NoNull(inpstr);
            if (!func.chkRegex(inpstr, @"^\d{1,8}(\.\d{1,2})?$")) func.Alert(tsstr + "必须为数字格式!", null);
            if (double.Parse(inpstr) <= 0) func.Alert(tsstr + "金额必须大于0!", null);
            return inpstr;
        }

        public static void wtfile(string path, string str)
        {
            using (StreamWriter w = new StreamWriter(path, false, Encoding.GetEncoding("utf-8")))
            {
                w.Write(str);
            }
        }

        public static string pager(int dqpg, int pgct, string url) //当前页码,总页码,后缀参数
        {
            if (dqpg > pgct) dqpg = pgct;
            string str = "第" + dqpg + "/" + pgct + "页&nbsp;&nbsp;";
            if (dqpg > 1)
                str += "<a href=?page=1" + url + ">[首页]</a>&nbsp;<a href=?page=" + (dqpg - 1) + url + ">[上页]</a>&nbsp;";
            else
                str += "[首页]&nbsp;[上页]&nbsp;";
            if (dqpg < pgct)
                str += "<a href=?page=" + (dqpg + 1) + url + ">[下页]</a>&nbsp;<a href=?page=" + pgct + url + ">[末页]</a>";
            else
                str += "[下页]&nbsp;[末页]";
            str += "&nbsp;到第<input name=\"page\" type=\"text\" id=\"page\" style=\"width:30px\" onKeyUp=\"value=value.replace(/[^0123456789]/g,'')\">页&nbsp;<input type=\"button\" name=\"Submit\" value=\"GO\" onClick=\"topage()\">\n<script type=\"text/JavaScript\">\nfunction topage(){\nvar pg=document.getElementById(\"page\").value;\nlocation.href=\"?page=\"+pg+\"" + url + "\"}\n</script>";
            return str;
        }

        public static string pager1(int dqpg, int pgct, string url) //当前页码,总页码,后缀参数
        {
            if (dqpg > pgct) dqpg = pgct;
            string str = "第" + dqpg + "/" + pgct + "页&nbsp;&nbsp;";
            if (dqpg > 1)
                str += "<a href=?page=1" + url + ">首页</a>&nbsp;<a href=?page=" + (dqpg - 1) + url + ">上页</a>&nbsp;";
            else
                str += "<span>首页</span>&nbsp;<span>上页</span>&nbsp;";
            if (dqpg < pgct)
                str += "<a href=?page=" + (dqpg + 1) + url + ">下页</a>&nbsp;<a href=?page=" + pgct + url + ">末页</a>";
            else
                str += "<span>下页</span>&nbsp;<span>末页</span>";
            str += "&nbsp;到第<input name=\"page\" type=\"text\" id=\"page\" style=\"width:30px\" onKeyUp=\"value=value.replace(/[^0123456789]/g,'')\">页&nbsp;<input type=\"button\" name=\"Submit\" value=\"GO\" onClick=\"topage()\">\n<script type=\"text/JavaScript\">\nfunction topage(){\nvar pg=document.getElementById(\"page\").value;\nlocation.href=\"?page=\"+pg+\"" + url + "\"}\n</script>";
            return str;
        }

        public static string makdir(string lj)
        {
            string sj = DateTime.Today.ToString("yyyy");
            lj = HttpContext.Current.Server.MapPath(lj) + sj;
            if (Directory.Exists(lj) == false) Directory.CreateDirectory(lj);
            return sj + "/";
        }

        public static void DDl(string str, DropDownList opj, string txtfil, string valfil)
        {
            IDataReader dr1 = db.dtReader(str);
            while (dr1.Read())
            {
                string stra = "";
                int cLevl = Convert.ToInt32(dr1["cLevl"]);
                if (cLevl == 1)
                    stra = "·";
                else
                    stra = stra.PadLeft(cLevl - 1).Replace(" ", "&nbsp;") + "├";
                opj.Items.Add(new ListItem(HttpContext.Current.Server.HtmlDecode(stra + dr1[txtfil].ToString()), dr1[valfil].ToString()));
            }
            dr1.Close();
        }

        public static string makpass(string s)   //生成加密密码
        {
            return func.PassStr("$*d8j@!f3" + s, "MD5");
        }

        public static string cookpas(string ps)   //生成加密cookis
        {
            return func.PassStr(ps + func.GetIP().Replace(".", ""), "MD5");
        }

        public static void sendMail(string tomail, string bt_str, string nr_str)
        {
            Random ran = new Random();
            string n = ran.Next(1, 6).ToString();
            n = "wsd" + n + "@x1.wsloan.com";

            string smtpstr = "smtp.mxhichina.com";
            string fromail = n;
            string mailuser = n;
            string mailpass = "Wsd7418521";
            //string mailprot = func.getConf("mailprot");

            MailMessage mailObj = new MailMessage();  //邮件内容
            mailObj.From = new MailAddress(fromail);
            mailObj.To.Add(tomail);
            mailObj.Subject = bt_str;
            mailObj.Body = nr_str;
            mailObj.BodyEncoding = Encoding.UTF8;
            mailObj.IsBodyHtml = true;
            mailObj.Priority = MailPriority.Normal;

            SmtpClient smtpurl = new SmtpClient(smtpstr);  //邮件操作
            //smtpurl.Port = int.Parse(mailprot);
            //smtpurl.EnableSsl = true;
            smtpurl.Credentials = new NetworkCredential(mailuser, mailpass);
            //smtpurl.Timeout = 60000;  //超时时间60秒
            smtpurl.Send(mailObj);
        }
    }
}